#include "insertdialog.h"
#include<QSqlError>
InsertDialog::InsertDialog(QString windowtitle,QWidget *parent) : QDialog(parent),windowtitle(windowtitle)
{
    this->setWindowTitle(windowtitle + "插入");
    query = new QSqlQuery();
    setMaximumSize(300,400);
    setMinimumSize(300,400);
    lineEidt1 = new QLineEdit(this);
    lineEidt1->setMaximumSize(100, 30);
    lineEidt2 = new QLineEdit(this);
    lineEidt2->setMaximumSize(100, 30);
    lineEidt3 = new QLineEdit(this);
    lineEidt3->setMaximumSize(100, 30);
    lineEidt4 = new QLineEdit(this);
    lineEidt4->setMaximumSize(100, 30);
    lineEidt5 = new QLineEdit(this);
    lineEidt5->setMaximumSize(100, 30);
    lineEidt6 = new QLineEdit(this);
    lineEidt6->setMaximumSize(100, 30);

    label1 = new QLabel("", this);
    label2 = new QLabel("", this);
    label3 = new QLabel("", this);
    label4 = new QLabel("", this);
    label5 = new QLabel("", this);
    label6 = new QLabel("", this);

    dateTime1 = new QDateTimeEdit(QDateTime::currentDateTime());
    dateTime1->setCalendarPopup(true);
    dateTime1->setMaximumWidth(200);
    dateTime2 = new QDateTimeEdit(QDateTime::currentDateTime());
    dateTime2->setCalendarPopup(true);
    dateTime2->setMaximumWidth(200);

    pushButton = new QPushButton("提交", this);
    addItems();
    addConnection();


}
void InsertDialog::addItems()
{
    QFormLayout* layout = new QFormLayout();
    if(windowtitle=="开出表")
    {
        label1->setText("开出ID");
        label2->setText("车位ID");
        label3->setText("开出人姓名");
        label4->setText("开出时间");
        label5->setText("开出车牌");

        lineEidt1->setMaxLength(2);
        lineEidt2->setMaxLength(2);
        lineEidt3->setMaxLength(5);
        lineEidt4->setMaxLength(7);


        layout->addRow(label1, lineEidt1);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label2, lineEidt2);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label3, lineEidt3);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label4, dateTime1);
        dateTime1->setCalendarPopup(true);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label5, lineEidt4);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addWidget(pushButton);

        label5->setVisible(true);
        label6->setVisible(false);
        lineEidt5->setVisible(false);
        lineEidt6->setVisible(false);
        dateTime1->setVisible(true);
        dateTime2->setVisible(false);
    }
    else if(windowtitle=="用户表")
    {
        label1->setText("用户ID");
        label2->setText("用户名");
        label3->setText("用户电话");
        label4->setText("用户车牌");

        lineEidt1->setMaxLength(2);
        lineEidt2->setMaxLength(4);
        lineEidt3->setMaxLength(11);
        lineEidt4->setMaxLength(7);

        layout->addRow(label1, lineEidt1);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label2, lineEidt2);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label3, lineEidt3);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label4, lineEidt4);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addWidget(pushButton);

        label5->setVisible(false);
        label6->setVisible(false);
        lineEidt5->setVisible(false);
        lineEidt6->setVisible(false);
        dateTime1->setVisible(false);
        dateTime2->setVisible(false);
    }
    else if(windowtitle=="预定表")
    {
        label1->setText("预定ID");
        label2->setText("预定开出时间");
        label3->setText("预定开入时间");
        label4->setText("预留电话");
        label5->setText("预定车位");

        lineEidt1->setMaxLength(2);
        lineEidt2->setMaxLength(11);
        lineEidt3->setMaxLength(2);

        layout->addRow(label1, lineEidt1);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label2, dateTime1);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label3, dateTime2);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label4, lineEidt2);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label5, lineEidt3);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addWidget(pushButton);

        label6->setVisible(false);

        lineEidt4->setVisible(false);
        lineEidt5->setVisible(false);
        lineEidt6->setVisible(false);

    }
    else if(windowtitle=="停车表")
    {
        label1->setText("停车ID");
        label2->setText("车位ID");
        label3->setText("预定开出时间");
        label4->setText("开入时间");
        label5->setText("车牌号");
        label6->setText("预留电话");

        layout->addRow(label1, lineEidt1);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label2, lineEidt2);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label3, dateTime1);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label4, dateTime2);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label5, lineEidt3);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label6, lineEidt4);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addWidget(pushButton);


        lineEidt5->setVisible(false);
        lineEidt6->setVisible(false);
    }
    else
    {
        label1->setText("车位ID");
        label2->setText("是否有车");
        label3->setText("离出口距离");
        label4->setText("价格");
        label5->setText("超时价格");

        layout->addRow(label1, lineEidt1);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label2, lineEidt2);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label3, lineEidt3);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label4, lineEidt4);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addRow(label5, lineEidt5);
        layout->addItem(new QSpacerItem(0, 40));
        layout->addWidget(pushButton);

        label6->setVisible(false);
        lineEidt6->setVisible(false);
        dateTime1->setVisible(false);
        dateTime2->setVisible(false);
    }

    this->setLayout(layout);
}


void InsertDialog::addConnection()
{
    connect(pushButton,&QPushButton::clicked, this, [=](){
        QString attribute;
        if(windowtitle=="开出表")
        {
            QString out_time = dateTime1->dateTime().toString("yyyy/MM/dd hh:mm:ss");
            attribute = QString("insert into 开出 values('%1', '%2', '%3', '%4' ,'%5');").arg(lineEidt1->text()).arg(lineEidt2->text()).arg(lineEidt3->text()).arg(out_time).arg(lineEidt4->text());
        }
        else if(windowtitle=="用户表")
        {
            attribute = QString("insert into 用户 values('%1', '%2', '%3', '%4')").arg(lineEidt1->text()).arg(lineEidt2->text()).arg(lineEidt3->text()).arg(lineEidt4->text());
        }
        else if(windowtitle=="停车表")
        {
            QString per_outtime = dateTime1->dateTime().toString("yyyy/MM/dd hh:mm:ss");
            QString in_time = dateTime2->dateTime().toString("yyyy/MM/dd hh:mm:ss");
            attribute = QString("insert into 停车 values('%1', '%2', '%3', '%4', '%5', '%6')").arg(lineEidt1->text()).arg(lineEidt2->text()).arg(per_outtime).arg(in_time).arg(lineEidt3->text()).arg(lineEidt4->text());
        }
        else if(windowtitle=="预定表")
        {
            QString pre_outtime = dateTime1->dateTime().toString("yyyy/MM/dd hh:mm:ss");
            QString pre_intime = dateTime2->dateTime().toString("yyyy/MM/dd hh:mm:ss");
            attribute = QString("insert into 预定 values('%1', '%2', '%3', '%4', '%5')").arg(lineEidt1->text()).arg(pre_outtime).arg(pre_intime).arg(lineEidt2->text()).arg(lineEidt3->text());

        }
        else
        {
            attribute = QString("insert into 车位 values('%1', '%2', '%3', '%4', '%5')").arg(lineEidt1->text().toInt()).arg(lineEidt2->text()).arg(lineEidt3->text()).arg(lineEidt4->text()).arg(lineEidt5->text());
        }
        qDebug()<<attribute;
        query->prepare(attribute);
        bool ok = query->exec();
        if(!ok)
        {
            QSqlError error = query->lastError();
            QMessageBox::warning(this,"插入失败",error.driverText(),QMessageBox::Ok);
            qDebug()<<query->lastError();

        }
        else
        {
            QMessageBox::information(this, tr("成功"),
                                 tr("插入成功"
                                    ""),
                                 QMessageBox::Ok);
            this->close();
        }
    });
}
